﻿namespace RexelProductenPortal.Controllers
{
    using System.Web.Mvc;
    using RexelProductenPortal.Models;
    using RexelProductenPortal.ViewModels;
    using System.Collections.Generic;
    using System.Configuration;

    public class ArticleController : Controller
    {
        //
        // GET: /Article/

        [HttpGet]
        [Authorize(Roles = "CreateEditArticle, ViewArticle, CreateArticle")] 
        public ActionResult Index(int? page)
        {
            var pageSizeValue = int.Parse(ConfigurationManager.AppSettings["articlePageSize"]);
            var pageindex = page.HasValue ? page.Value : 1;
            var articles = new List<VMArticle>();
            dynamic table = new Article(); //"dynamic" is important here - don't use "var"!
            var d_articles = table.Paged(where: "fabrikaat = @0", args: "ABB", columns: @"Artikelnaam, 
                [Artikelnummer fabrikant] as ArticleNumberManufacturer, [Serie/programma] as Series",
                currentPage: pageindex, pageSize: pageSizeValue, orderBy: "[Artikelnummer fabrikant]");
            CreatePaging(page, d_articles);
            foreach (var article in d_articles.Items)
            {
                var tmparticle = new VMArticle();
                tmparticle.ArticleName = article.Artikelnaam;
                tmparticle.ArticleNumberManufacturer = article.ArticleNumberManufacturer;
                tmparticle.Series = article.Series;
                articles.Add(tmparticle);
            }
            return View(articles);
        }

        private void CreatePaging(int? page, dynamic d_articles)
        {
            ViewBag.minPage = (page.HasValue && page.Value - 3 > 0) ? (page.Value - 3) : 1;
            ViewBag.maxPage = (page.HasValue && page.Value + 5 < d_articles.TotalRecords) ? (page.Value + 5) : 5;
            ViewBag.currentPage = page;
        }

        [HttpGet]
        [Authorize(Roles = "CreateEditArticle, ViewArticle, CreateArticle")] 
        public ActionResult Details(string id)
        {
            dynamic table = new Article(); //"dynamic" is important here - don't use "var"!
            var article = table.Single(where: "[Artikelnummer fabrikant] = @0", args: id);
            var tmparticle = new VMArticle();
            tmparticle.ArticleName = article.Artikelnaam;
            tmparticle.ArticleNumberManufacturer = (string)((IDictionary<string, object>)article)["Artikelnummer fabrikant"];
            tmparticle.Series = (string)((IDictionary<string, object>)article)["Serie/programma"];
            return View(tmparticle);
        }

        [HttpGet]
        [Authorize(Roles = "CreateEditArticle, CreateArticle")] 
        public ActionResult New()
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "CreateEditArticle,CreateArticle")] 
        public ActionResult New(VMArticle model)
        {
            var table = new Article();
            var newID = table.Insert(new { 
                Artikelnaam = model.ArticleName, 
                Artikelnummer_fabrikant = model.ArticleNumberManufacturer
            }, true);
            return (Index(1));
        }

        [HttpGet]
        [Authorize(Roles = "CreateEditArticle")] 
        public ActionResult Edit(string id)
        {
            dynamic table = new Article(); //"dynamic" is important here - don't use "var"!
            dynamic article = table.Single(select: @"Artikelnaam, [Artikelnummer fabrikant] as ArticleNumberManufacturer, 
                [Serie/programma] as Series", where: "[Artikelnummer fabrikant] = @0", args: id);
            var tmparticle = new VMArticle();
            tmparticle.ArticleName = article.Artikelnaam;
            tmparticle.ArticleNumberManufacturer = article.ArticleNumberManufacturer;
            tmparticle.Series = article.Series;
            return View(tmparticle);
        }

        [HttpPost]
        [Authorize(Roles = "CreateEditArticle")] 
        public ActionResult Edit(VMArticle model)
        {
            var table = new Article();
            var newID = table.Update(new
            {
                ArticleNumberManufacturer = model.ArticleNumberManufacturer
            }, model.ArticleNumberManufacturer);
            return (Index(1));
        }
    }
}
